package queue;

import java.util.PriorityQueue;

public class LastStoneWeight {
    public static int code(int[] stones) {
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);

        for (int x : stones){
            heap.offer(x);
        }

        while (heap.size() > 1){
            int a = heap.poll();
            int b = heap.poll();

            if (a > b){
                heap.offer(a - b);
            }
        }

        return heap.isEmpty() ? 0 : heap.peek();
    }

    public static void main(String[] args) {
        LastStoneWeight.code(new int[]{1, 2, 3, 4});
    }
}
